<template>
  <div>
    <van-nav-bar title="用户登录" left-arrow @click-left="onClickLeft" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="手机号"
        placeholder="手机号"
        :rules="[{ required: true, message: '请填写手机号' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
    <p @click="toRegister" class="to-register">还没有账号?请先注册</p>
  </div>
</template>

<script>
import { getUserByUsername } from "@/api/user.js";
import Vue from "vue";
import { Form } from "vant";
import { Toast } from "vant";

Vue.use(Toast);
Vue.use(Form);
export default {
  data() {
    return {
      username: "",
      password: ""
    };
  },
  created() {},
  computed: {},
  methods: {
    async onSubmit(values) {
      console.log(values);
      let rs = await getUserByUsername(values);
      if (rs.status === 200) {
        console.log(rs);
        if (rs.data.length === 0) {
          Toast("用户名或密码错误 请重试");
        } else {
          localStorage.setItem("username", values.username);
          // localStorage.setItem("id", values.id);
          // localStorage.getItem("username");
          Toast("登录成功");
          this.$router.push("/redwine/home");
        }
      }
    },
    toRegister() {
      this.$router.push("/register");
    },
    // 返回上一页
    onClickLeft() {
      this.$router.go(-1);
    }
  }
};
</script>

<style lang="stylus" scoped>
.van-nav-bar >>> .van-icon {
  color: #0f0f0f !important;
}

.to-register {
  text-decoration: underline;
  color: #555;
  text-align: right;
  padding-right: 0.5rem;

  &:hover {
    color: red;
  }
}

>>> .van-button--info {
  background-color: #c3a769;
  border: none;
}
</style>
